package com.macro.mall.controller;

import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.model.UmsRole;
import com.macro.mall.model.WmsInStorage;
import com.macro.mall.model.WmsInStorageItem;
import com.macro.mall.service.UmsAdminService;
import com.macro.mall.wms.dto.InStorageDto;
import com.macro.mall.wms.dto.OutStorageDto;
import com.macro.mall.wms.dto.WmsInStorageItemDTO;
import com.macro.mall.wms.dto.WmsOutStorageItemDTO;
import com.macro.mall.wms.service.IInStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/wmsIn")
public class WmsInStorageController {

    @Autowired
    private IInStorageService inStorageService;
    @Autowired
    private UmsAdminService adminService;

    /**
     * 根据入库单id获取出库单及子入库单信息
     *
     * @param orderId 入库单id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInStorageItems", method = RequestMethod.GET)
    public CommonResult<List<WmsInStorageItemDTO>> getInStorageItems(@RequestParam Long orderId) {
        List<WmsInStorageItemDTO> inStorageList = inStorageService.getInStorageItems(orderId);
        return CommonResult.success(inStorageList);
    }

    /**
     * 查询门店所有入库单
     *
     * @param wmsInStorage 入库单对象（可模糊查询）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInStorageList", method = RequestMethod.GET)
    public CommonResult<CommonPage<WmsInStorage>> getInStorageList(@CurrentUserName String currentUsername,
                                                                   WmsInStorage wmsInStorage,
                                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUsername);
        List<WmsInStorage> inStorageList = inStorageService.getInStorageList(admin.getStoreId(), wmsInStorage, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(inStorageList));
    }

    /**
     * 根据id查询入库单详情
     *
     * @param id 入库单id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInStorage", method = RequestMethod.GET)
    public CommonResult getInStorage(@RequestParam Long id) {
        return inStorageService.getInStorage(id);
    }

    /**
     * 创建入库单
     *
     * @param currentUserName
     * @param inStorageDto
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/createInStorage", method = RequestMethod.POST)
    public CommonResult createInStorage(@CurrentUserName String currentUserName, @RequestBody InStorageDto inStorageDto) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUserName);
        return inStorageService.createInStorage(admin.getId(), admin.getUserName(), admin.getStoreId(), inStorageDto);
    }

    /**
     * 未审核修改入库单
     *
     * @param id              入库单id
     * @param currentUserName admin账号信息
     * @param inStorageDto            入库商品单list集合
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cudInStorage/{id}", method = RequestMethod.POST)
    public CommonResult cudInStorage(@PathVariable(value = "id") Long id, @CurrentUserName String currentUserName,
                                     @RequestBody InStorageDto inStorageDto) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUserName);
        return inStorageService.cudInStorage(admin.getId(), admin.getUserName(), admin.getStoreId(), id, inStorageDto);
    }

    /**
     * 审核入库单，修改库存
     *
     * @param id              入库单id
     * @param currentUserName admin账号信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/auditInStorage", method = RequestMethod.POST)
    public CommonResult auditInStorage(@RequestParam Long id, @CurrentUserName String currentUserName) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUserName);
        return inStorageService.auditInStorage(admin.getId(), admin.getUserName(), admin.getStoreId(), id);
    }

    /**
     * 删除未审核的入库单
     *
     * @param id              入库单id
     * @param currentUserName admin账号信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteInStorage", method = RequestMethod.POST)
    public CommonResult deleteInStorage(@RequestParam Long id, @CurrentUserName String currentUserName) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUserName);
        return inStorageService.deleteInStorage(id, admin.getId());
    }

}
